package org.wedding.center.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.web.bind.annotation.*;
import org.wedding.center.dto.query.MerchantQuery;
import org.wedding.center.dto.query.ServiceQuery;
import org.wedding.center.dto.vo.MerchantVo;
import org.wedding.center.entity.ServiceInfo;
import org.wedding.center.service.ServiceInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.wedding.utils.CommonResult;
import org.wedding.dto.PageModel;

import java.util.List;

/**
 * 服务信息表(ServiceInfo)表控制层
 *
 * @author songkaiyue
 * @since 2025-05-14 16:30:24
 */
@AllArgsConstructor
@RestController
@RequestMapping("/serviceInfo")
@Tag(name = "ServiceInfo", description = "服务信息表")
public class ServiceInfoController{
    private final ServiceInfoService serviceInfoService;

    /**
     * 小程序首页 查询商家列表信息
     * @return 所有数据
     */
    @SaIgnore
    @Operation(summary = "小程序首页 查询商家列表信息")
    @RequestMapping(value = "/merchants", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json; charset=UTF-8")
    public CommonResult merchants(MerchantQuery query) {
        CommonResult<List<MerchantVo>> success = CommonResult.success(serviceInfoService.findMerchants(query));
        return success;
    }
    /**
     * 分页查询所有数据
     *
     * @param query 分页对象
     * @return 所有数据
     */
    @Operation(summary = "分页查询所有服务信息数据")
    @RequestMapping(value = "/list", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json; charset=UTF-8")
    public CommonResult<PageModel> list(ServiceQuery query) {
        return serviceInfoService.findAll(query);
    }


    /**
     * 分页查询所有数据
     *
     * @param query 分页对象
     * @return 所有数据
     */
    @Operation(summary = "分页查询所有服务信息数据")
    @RequestMapping(value = "/listsystem", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json; charset=UTF-8")
    public CommonResult<PageModel> listsystem(ServiceQuery query) {
        String role = StpUtil.getSession().get("role").toString();
        System.out.println(role);
        String userId = StpUtil.getSession().get("userId").toString();
         if("1".equals(role)) {
            //若是商家则查询自己的订单
            query.setUserId(userId);
         }
        return serviceInfoService.findAll(query);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @Operation(summary = "通过主键查询单条数据")
    @RequestMapping(value = "/view", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json; charset=UTF-8")
    public CommonResult view(String id) {
        return serviceInfoService.view(id);
    }

    /**
     * 根据id查询商家信息
     * @param id
     * @return
     */
    @Operation(summary = "根据id查询商家信息")
    @RequestMapping(value = "/findMerchantById", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json; charset=UTF-8")
    public CommonResult findMerchantById(String id, String latitude, String longitude) {
        return serviceInfoService.findMerchantById(id, latitude, longitude);
    }

    /**
     * 新增数据
     *
     * @param serviceInfo 实体对象
     * @return 新增结果
     */
    @Operation(summary = "新增数据")
    @RequestMapping(value = "/add", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json; charset=UTF-8")
    public CommonResult add(@RequestBody ServiceInfo serviceInfo) {
        return serviceInfoService.add(serviceInfo);
    }

    /**
     * 修改数据
     *
     * @param serviceInfo 实体对象
     * @return 修改结果
     */
    @Operation(summary = "修改数据")
    @RequestMapping(value = "/update", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json; charset=UTF-8")
    public CommonResult update(@RequestBody ServiceInfo serviceInfo) {
        return serviceInfoService.updateEntity(serviceInfo);
    }

   /**
     * 删除数据
     *
     * @param ids 主键
     * @return 删除结果
     */
    @Operation(summary = "删除数据")
    @RequestMapping(value = "/delete", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json; charset=UTF-8")
    public CommonResult delete(@RequestBody List<String> ids) {
        return serviceInfoService.delete(ids);
    }

}

